/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package client.views;


import client.beans.AuthBean;
import client.beans.ServerBean;
import client.types.Account;
import client.types.Transaction;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.xml.bind.JAXBException;
import server.entities.User;
import types.ResponseXML;

/**
 *
 * @author efim
 */
@ManagedBean
@SessionScoped
public class PanelView{
    @EJB
    private AuthBean authBean;
    @EJB
    private ServerBean serverBean;
    
    
    /**
     * Creates a new instance of PanelView
     */
    public PanelView() {
       accounts = new LinkedList<>();
       transactions = new LinkedList<>();
    }
    
    private List<Account> accounts;

    /**
     * Get the value of accounts
     *
     * @return the value of accounts
     */
    public List<Account> getAccounts() {
        return accounts;
    }

    private User user;

    /**
     * Get the value of user
     *
     * @return the value of user
     */
    public User getUser() {
        return user;
    }

    /**
     * Set the value of user
     *
     * @param user new value of user
     */
    public void setUser(User user) {
        this.user = user;
    }

    /**
     * Set the value of accounts
     *
     * @param accounts new value of accounts
     */
    public void setAccounts(List<Account> accounts) {
        this.accounts = accounts;
    }
    
    private List<Transaction> transactions;

    /**
     * Get the value of transactions
     *
     * @return the value of transactions
     */
    public List<Transaction> getTransactions() {
        return transactions;
    }

    /**
     * Set the value of transactions
     *
     * @param transactions new value of transactions
     */
    public void setTransactions(List<Transaction> transactions) {
        this.transactions = transactions;
    }

    @PostConstruct
    public void Init()
    {
        InputStream is = null;
        try {
            Map<String,String> params = new HashMap<>();
            params.put("user_id", String.valueOf(authBean.getUserId()));
            Object answer = serverBean.queryToServer("GetAccounts", params);
            ResponseXML<Account> acc = (ResponseXML<Account>)answer;
            accounts = acc.getData().getItems();
            
            params = new HashMap<>();     
            params.put("account_ids", "0,1,2");
            answer = serverBean.queryToServer("GetTransactions", params);
            ResponseXML<Transaction> tt = (ResponseXML<Transaction>)answer;
            transactions = tt.getData().getItems(); 
            
            params = new HashMap<>();     
            params.put("id", String.valueOf(authBean.getUserId()));
            answer = serverBean.queryToServer("GetUsers", params);
            ResponseXML<User> u = (ResponseXML<User>)answer;
            user = u.getData().getItems().get(0);
            
        } catch (JAXBException ex) {
            Logger.getLogger(AddTransactionView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(AddTransactionView.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }    
    
    
    public double getAmount()
    {
        double result = 0.0;
        for(Transaction t: transactions)
        {
            result += t.getAmount();
        }
        return result;
    }
    
    
}
